<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 2016/10/31
 * Time: 下午3:07
 */

$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

//获取所有的 执行状态的pos 退款

get_supply_stock_new("2018-02-01 00:00:00","2018-02-28 23:59:59",0,7);

function get_supply_stock_new($begin_date,$end_date,$data_id,$shop_id){
    echo 'start mem '.round(memory_get_usage()/1024/1024, 2)."\n";
    $finally=array();
    $begin_date_new=date("Y-m-d H:i:s",strtotime($begin_date)-Config::get("config.report_time_diff"));
    $end_date_new=date("Y-m-d H:i:s",strtotime($end_date)-Config::get("config.report_time_diff"));

    //获取 该库的期初库存
    $start_stock_array=array();
    // $start_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($begin_date_new)))->where("data_id",$data_id)->get();
    $time=time();
    $start_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($begin_date_new)-86400))->get();
    $time1=time();
    echo ($time1-$time)."\n";
    foreach ($start_stock as $_start_stock) {
        if(isset($start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id])){
            $temp=$start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id];
            $temp->number=$temp->number+$_start_stock->number;
            $temp->amount=$temp->amount+$_start_stock->amount;
            $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$temp;
        }else{
            $start_stock_array[$_start_stock->item_type.'-'.$_start_stock->item_id]=$_start_stock;
        }

    }
    $time2=time();
    echo ($time2-$time1)."\n";
    // //入库数据
    // $storage_data = Report::get_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
    // // var_dump($storage_data);exit;
    // //服务订单维修出库
    // $service_order_out_data = Report::get_service_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    // //零售订单销售出库
    // $sale_order_out_data = Report::get_sale_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    // //门店间内部调出出库
    // $transfer_out_data = Report::get_transfer_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
    // /*供货商退货出库*/
    // $supplier_out_data = Report::get_supplier_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
    // //盘点盘盈调整入库
    // $inventory_storage_data = Report::get_inventory_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
    // //盘点盘亏调整出库
    // $inventory_out_data = Report::get_inventory_out_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);
    // //结算后退货的
    // $return_stireage_date = Report::get_return_storage_data_for_jxc($shop_id,$data_id,$begin_date_new,$end_date_new);


    //入库数据
    $storage_data = ReportDateReocrd::get_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    // var_dump($storage_data);exit;
    //服务订单维修出库
    $service_order_out_data = ReportDateReocrd::get_service_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    //零售订单销售出库
    $sale_order_out_data = ReportDateReocrd::get_sale_order_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    //门店间内部
    $transfer_out_data = ReportDateReocrd::get_transfer_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);

    //门店间内部领用领用
    $transfer_out_data_ly = ReportDateReocrd::get_transfer_out_data_for_jxc_ly($shop_id,$data_id,$begin_date,$end_date);
    //门店间内部调出出库
    $transfer_out_data_dc = ReportDateReocrd::get_transfer_out_data_for_jxc_dc($shop_id,$data_id,$begin_date,$end_date);
    //门店间内部调出出库
    $transfer_out_data_xs = ReportDateReocrd::get_transfer_out_data_for_jxc_xs($shop_id,$data_id,$begin_date,$end_date);

    /*供货商退货出库*/
    $supplier_out_data = ReportDateReocrd::get_supplier_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    //盘点盘盈调整入库
    $inventory_storage_data = ReportDateReocrd::get_inventory_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    //盘点盘亏调整出库
    $inventory_out_data = ReportDateReocrd::get_inventory_out_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    //结算后退货的
    $return_stireage_date = ReportDateReocrd::get_return_storage_data_for_jxc($shop_id,$data_id,$begin_date,$end_date);
    $time3=time();
    echo ($time3-$time2)."\n";

    //获取 该库的期末库存
    $end_stock_array=array();
    // $end_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($end_date_new)))->where("data_id",$data_id)->get();
    $end_stock=MaintenanceWarehouseOrderStock::where("shop_id",$shop_id)->where("date",date("Y-m-d",strtotime($end_date_new)))->get();
    foreach ($end_stock as $_end_stock) {
        if(isset($end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id])){
            $temp=$end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id];
            $temp->number=$temp->number+$_end_stock->number;
            $temp->amount=$temp->amount+$_end_stock->amount;
            $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$temp;
        }else{
            $end_stock_array[$_end_stock->item_type.'-'.$_end_stock->item_id]=$_end_stock;
        }
    }
    $time4=time();
    echo ($time4-$time3)."\n";
    $maintenance_ware_house = MaintenanceWarehouse::where("shop_id",$shop_id)->where('created_at','<=',$end_date)->get();
    foreach ($maintenance_ware_house as $_maintenance_ware_house){
        $key=$_maintenance_ware_house->item_type.'-'.$_maintenance_ware_house->item_id;
 
        $item_info = SupplyItem::find($_maintenance_ware_house->item_id);
        $part_classification = Report::get_part_classification($item_info->supply_classification_id);
        $start_number=isset($start_stock_array[$key]->number)?$start_stock_array[$key]->number:0;
        $start_amount=isset($start_stock_array[$key]->amount)?$start_stock_array[$key]->amount:0;
        $duo=isset($inventory_storage_data[$key]['number'])?$inventory_storage_data[$key]['number']:0;
        $duo_amount=isset($inventory_storage_data[$key]['tax_purchase_subtotal'])?$inventory_storage_data[$key]['tax_purchase_subtotal']:0;

        $shao=isset($inventory_out_data[$key]['number'])?$inventory_out_data[$key]['number']:0;
        $shao_amount=isset($inventory_out_data[$key]['tax_purchase_subtotal'])?$inventory_out_data[$key]['tax_purchase_subtotal']:0;

        //获取 时间段内的入库成本
        $temp_array=array(
            'item_type' => $_maintenance_ware_house->item_type,
            'item_id' => $_maintenance_ware_house->item_id,
            'item_brand_name' => $item_info->brand_name,
            'item_series_name' => $item_info->series_name,
            'item_model' => $item_info->model,
            'item_code' => $item_info->code,
               // 'item_specification' => $item_info->specification,
               // 'item_bz_unit' => $item_info->bz_unit,
            'item_pack' => $item_info->volume_size,
            'item_name' => $item_info->name,
               // 'item_unit' => $item_info->unit,
            'st_classifi_id' => $item_info->supply_classification_id,
            'level_1_id' => $part_classification['level_1_id'],
            'level_1_name' => $part_classification['level_1_name'],
            'level_2_id' => $part_classification['level_2_id'],
            'level_2_name' => $part_classification['level_2_name'],
            'level_3_id' => $part_classification['level_3_id'],
            'level_3_name' => $part_classification['level_3_name'],
            'start_stock' => $start_number,
            'start_amount' =>$start_amount,
            'into_stock' => isset($storage_data[$key]['number'])?$storage_data[$key]['number']:0,
            'into_amount' => isset($storage_data[$key]['tax_purchase_subtotal'])?$storage_data[$key]['tax_purchase_subtotal']:0,
            'return_into_stock' => isset($return_stireage_date[$key]['number'])?$return_stireage_date[$key]['number']:0,
            'return_into_amount' => isset($return_stireage_date[$key]['tax_purchase_subtotal'])?$return_stireage_date[$key]['tax_purchase_subtotal']:0,
            'orderout_stock' => isset($service_order_out_data[$key]['number'])?$service_order_out_data[$key]['number']:0,
            'orderout_amount' => isset($service_order_out_data[$key]['tax_purchase_subtotal'])?$service_order_out_data[$key]['tax_purchase_subtotal']:0,
            'saleout_stock' => isset($sale_order_out_data[$key]['number'])?$sale_order_out_data[$key]['number']:0,
            'saleout_amount' => isset($sale_order_out_data[$key]['tax_purchase_subtotal'])?$sale_order_out_data[$key]['tax_purchase_subtotal']:0,
            'diaochu_stock' => isset($transfer_out_data[$key]['number'])?$transfer_out_data[$key]['number']:0,
            'diaochu_amount' => isset($transfer_out_data[$key]['tax_purchase_subtotal'])?$transfer_out_data[$key]['tax_purchase_subtotal']:0,

            'diaochu_stock_ly' => isset($transfer_out_data_ly[$key]['number'])?$transfer_out_data_ly[$key]['number']:0,
            'diaochu_amount_ly' => isset($transfer_out_data_ly[$key]['tax_purchase_subtotal'])?$transfer_out_data_ly[$key]['tax_purchase_subtotal']:0,
            'diaochu_stock_dc' => isset($transfer_out_data_dc[$key]['number'])?$transfer_out_data_dc[$key]['number']:0,
            'diaochu_amount_dc' => isset($transfer_out_data_dc[$key]['tax_purchase_subtotal'])?$transfer_out_data_dc[$key]['tax_purchase_subtotal']:0,
            'diaochu_stock_xs' => isset($transfer_out_data_xs[$key]['number'])?$transfer_out_data_xs[$key]['number']:0,
            'diaochu_amount_xs' => isset($transfer_out_data_xs[$key]['tax_purchase_subtotal'])?$transfer_out_data_xs[$key]['tax_purchase_subtotal']:0,

            'pandian_stock' => $duo-$shao,
            'pandian_amount' => $duo_amount-$shao_amount,
            'supplier_stock' => isset($supplier_out_data[$key]['number'])?$supplier_out_data[$key]['number']:0,
            'supplier_amount' => isset($supplier_out_data[$key]['tax_purchase_subtotal'])?$supplier_out_data[$key]['tax_purchase_subtotal']:0,
            'end_stock' => isset($end_stock_array[$key]->number)?$end_stock_array[$key]->number:0,
            'end_amount' => isset($end_stock_array[$key]->amount)?$end_stock_array[$key]->amount:0,
        );
        // $temp_array["end_stock"]=$temp_array["start_stock"]+$temp_array["into_stock"]-$temp_array["orderout_stock"]-$temp_array["saleout_stock"]-$temp_array["diaochu_stock"]+$temp_array["pandian_stock"]-$temp_array["supplier_stock"];
        // $temp_array["end_amount"]=$temp_array["start_amount"]+$temp_array["into_amount"]-$temp_array["orderout_amount"]-$temp_array["saleout_amount"]-$temp_array["diaochu_amount"]+$temp_array["pandian_amount"]-$temp_array["supplier_amount"];
        $finally[]=$temp_array;
    }
    unset($maintenance_ware_house);
    unset($end_stock);
    unset($end_stock_array);
    unset($return_stireage_date);
    unset($inventory_out_data);
    unset($inventory_storage_data);
    unset($supplier_out_data);
    unset($transfer_out_data_xs);
    unset($transfer_out_data_dc);
    unset($transfer_out_data_ly);
    unset($transfer_out_data);
    unset($sale_order_out_data);
    unset($service_order_out_data);
    unset($storage_data);
    unset($start_stock_array);
    unset($start_stock);
    $time5=time();
    echo ($time5-$time4)."\n";
    echo 'end mem'.round(memory_get_usage()/1024/1024, 2)."\n";
    echo 'record size '.sizeof($finally);
    return $finally;
}

